package com.eascs.admin.o2o.loan.controller;

import java.math.BigDecimal;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.admin.o2o.internal.entity.LoanapplyExpand;
import com.eascs.admin.o2o.internal.service.LoanapplyExpandService;
import com.eascs.admin.o2o.internal.util.StatusContexts;
import com.eascs.admin.o2o.loan.service.O2oChargeSituationService;
import com.eascs.admin.o2o.loanAccount.service.IO2oCollectFeesService;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.HtmlRestResult;
import com.eascs.site.view.ViewContext;

@RestController
@RequestMapping("/rcharge")
public class O2oChargeSituationRestController {
	private static Logger logger = LoggerFactory.getLogger(O2oChargeSituationRestController.class);
	@Autowired
	private LoanapplyExpandService loanapplyExpandService;
	@Autowired
	private O2oChargeSituationService o2oChargeSituationService;

	/**
	 * 显示扣费界面
	 * 
	 * @param loanapplyNo
	 * @return
	 */
	@RequestMapping("/view")
	public BaseRestResult<String> view(String loanapplyNo) {
		logger.info("访问显示扣费界面接口开始");
		logger.info("查询贷款保证金开始");
		LoanapplyExpand expand = o2oChargeSituationService.queryExpandByLoanapplyNo(loanapplyNo);
		logger.info("查询贷款保证金信息:{}", expand);
		BigDecimal freezeBalance = o2oChargeSituationService.getFreezeBalance(loanapplyNo);
		ViewContext.put("amountAvailable", freezeBalance);
		ViewContext.put("loanapplyNo", loanapplyNo);
		ViewContext.put("expand", expand);
		return new HtmlRestResult("oldo2olike/mqsb/charge_situation");
	}

	/**
	 * 扣费按钮
	 * 
	 * @param loanapplyNo
	 * @return
	 */
	@RequestMapping("/fee_deduction")
	public BaseRestResult<String> fee_deduction(String loanapplyNo) {
		logger.info("查询贷款保证金开始");
		LoanapplyExpand loanapplyExpand = loanapplyExpandService.queryLoanapplyNo(loanapplyNo);
		LoanapplyExpand expand = o2oChargeSituationService.queryExpandByLoanapplyNo(loanapplyNo);
		logger.info("查询贷款保证金信息:{}", expand);
		String type = "TF";// 退费
		if (loanapplyExpand == null || (StatusContexts.WFTYPE_STATUS_RUN.equals(loanapplyExpand.getBillStatus()) || StatusContexts.WFTYPE_STATUS_NEW.equals(loanapplyExpand.getBillStatus()))) {
			type = "KF";// 扣费
			String message = o2oChargeSituationService.Charging(expand, type);
			if ("SUCCESS".equals(message))
				return BaseRestResult.info("扣费成功").andJquerySelectAjaxAction("#rcharge_uploader");
			else
				return BaseRestResult.warn(message).andJquerySelectAjaxAction("#rcharge_uploader");
		}
		boolean issuccess = (loanapplyExpand.getRecDeposite().compareTo(expand.getRedDeposite()) == 0) && (loanapplyExpand.getRecManger().compareTo(expand.getRedManger()) == 0) && (loanapplyExpand.getRecRisk().compareTo(expand.getRedRisk()) == 0);
		if (issuccess) {
			return BaseRestResult.warn("已扣费成功，不可重复点击扣费！").andJquerySelectAjaxAction("#rcharge_uploader");
		} else {
			String result = o2oChargeSituationService.Charging(loanapplyExpand, type);// 退费
			if ("SUCCESS".equals(result)) {
				type = "KF";// 扣费
				String message = o2oChargeSituationService.Charging(expand, type);
				if ("SUCCESS".equals(message))
					return BaseRestResult.info("扣费成功").andJquerySelectAjaxAction("#rcharge_uploader");
				else
					return BaseRestResult.warn(message).andJquerySelectAjaxAction("#rcharge_uploader");
			} else {
				return BaseRestResult.warn(result).andJquerySelectAjaxAction("#rcharge_uploader");
			}
		}
	}
	
	@RequestMapping("/test")
	public BaseRestResult<String> test(){
		o2oChargeSituationService.queryListCharge();
		return BaseRestResult.buildSuccessResult();
	}
	
	/**
	 * 
	 */
	@Reference
	IO2oCollectFeesService collectFeesService;
	@RequestMapping("/testCollectFee")
	public BaseRestResult<String> testCollectFee(){
		collectFeesService.queryListCollectFee();
		return BaseRestResult.buildSuccessResult();
	}
	
}
